#include<bits/stdc++.h>
using namespace std;

struct node
{
	int m, n, h;
	bool operator < (const node &x) const
	{
		return h < x.h;
	}
}b[10010];

int a[105][105];
int h[105][105]; 

int main()
{
	int m, n;
	cin>>m>>n;
	int k = 1;
	for(int i = 1;i <= m;i++)
	{
		for(int j = 1;j <= n;j++)
		{
			cin>>a[i][j];
			h[i][j] = 1;
			b[k++] = {i, j, a[i][j]};
		}
	}
	sort(b+1, b+k);
	for(int i = 1;i < k;i++)
	{
		node t = b[i];
		if(t.m > 1 && a[t.m-1][t.n] < a[t.m][t.n])
			h[t.m][t.n] = max(h[t.m][t.n], h[t.m-1][t.n]+1);
		if(t.m < m && a[t.m+1][t.n] < a[t.m][t.n])
			h[t.m][t.n] = max(h[t.m][t.n], h[t.m+1][t.n]+1);
		if(t.n > 1 && a[t.m][t.n-1] < a[t.m][t.n])
			h[t.m][t.n] = max(h[t.m][t.n], h[t.m][t.n-1]+1);
		if(t.n < n && a[t.m][t.n+1] < a[t.m][t.n])
			h[t.m][t.n] = max(h[t.m][t.n], h[t.m][t.n+1]+1);
	}
	cout<<*max_element(&h[1][1], &h[m][n+1]);
	return 0;
}

